package com.itheima.controller;

import com.itheima.anno.Log;
import com.itheima.mapper.DeptMapper;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j

@RequestMapping("/depts")
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    @Log
    @GetMapping
    public Result list() {
        log.info("查询数据");
        List<Dept> deptList = deptService.list();
        return Result.success(deptList);
    }

    // 根据id删除部门信息
    @Log
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id){
        deptService.delete(id);
        return Result.success();
    }

    //添加部门
    @Log
    @PostMapping
    public Result add(Dept dept){
        deptService.add(dept);
        return Result.success();
    }

    //根据id查询部门信息
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        Dept dept = deptService.query(id);
        return Result.success(dept);
    }

    //修改部门信息
    @Log
    @PutMapping
    public Result update(Dept dept){
        deptService.update(dept);
        return Result.success();
    }



}
